package com.cleaningcloud.erp.service.impl;

import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.core.utils.UniqueIdUtils;
import com.cleaningcloud.common.security.utils.CommunityTypeConversionUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import com.cleaningcloud.erp.api.domain.ErpCommodityStockAlter;
import com.cleaningcloud.erp.api.domain.ErpWareCommodityNumber;
import com.cleaningcloud.erp.api.utils.StockCommodityAlterUtils;
import com.cleaningcloud.erp.domain.ErpWareOverflow;
import com.cleaningcloud.erp.domain.dto.ErpWareOverflowDto;
import com.cleaningcloud.erp.domain.vo.ErpWareOverflowDetailsVo;
import com.cleaningcloud.erp.domain.vo.ErpWareOverflowVo;
import com.cleaningcloud.erp.mapper.ErpCommodityStockAlterMapper;
import com.cleaningcloud.erp.mapper.ErpWareCommodityNumberMapper;
import com.cleaningcloud.erp.mapper.ErpWareOverflowMapper;
import com.cleaningcloud.erp.service.IErpWareOverflowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 报溢Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-12-02
 */
@Service
public class ErpWareOverflowServiceImpl implements IErpWareOverflowService {
    @Autowired
    private ErpWareOverflowMapper erpWareOverflowMapper;

    @Autowired
    private ErpWareCommodityNumberMapper erpWareCommodityNumberMapper;

    @Autowired
    private ErpCommodityStockAlterMapper erpCommodityStockAlterMapper;

    /**
     * 查询报溢
     *
     * @param wareOverflowId 报溢主键
     * @return 报溢
     */
    @Override
    public ErpWareOverflowVo selectErpWareOverflowByWareOverflowId(Long wareOverflowId) {
        ErpWareOverflowVo erpWareOverflowVo = erpWareOverflowMapper.selectErpWareOverflowByWareOverflowId(wareOverflowId);
        for (ErpWareOverflowDetailsVo erpWareOverflowDetailsVo : erpWareOverflowVo.getDetailsList()) {
            erpWareOverflowDetailsVo.setCommodityType(CommunityTypeConversionUtils.conversion(erpWareOverflowDetailsVo.getCommodityType()));
        }
        return erpWareOverflowVo;
    }

    /**
     * 查询报溢列表
     *
     * @param erpWareOverflow 报溢
     * @return 报溢
     */
    @Override
    public List<ErpWareOverflowVo> selectErpWareOverflowList(ErpWareOverflow erpWareOverflow) {
        return erpWareOverflowMapper.selectErpWareOverflowList(erpWareOverflow);
    }

    /**
     * 新增报溢
     *
     * @param erpWareOverflow 报溢
     * @return 结果
     */
    @Override
    @Transactional
    public int insertErpWareOverflow(ErpWareOverflowDto erpWareOverflow) {
        erpWareOverflow.setCreateTime(DateUtils.getNowDate());
        erpWareOverflow.setCreateId(SecurityUtils.getUserId());
        erpWareOverflow.setCreateName(SecurityUtils.getUsername());
        erpWareOverflow.setApproveStatus(erpWareOverflow.getApproveStatus() == null ? 0 : erpWareOverflow.getApproveStatus());
        erpWareOverflow.setFlag("Y");
        erpWareOverflow.setWareOverflowOdd("BY" + UniqueIdUtils.generateUniqueId());
        erpWareOverflowMapper.insertErpWareOverflow(erpWareOverflow);
        if (erpWareOverflow.getDetailsList() != null && !erpWareOverflow.getDetailsList().isEmpty()) {
            erpWareOverflow.getDetailsList().forEach(item -> {
                item.setWareOverflowId(erpWareOverflow.getWareOverflowId());
            });
            erpWareOverflowMapper.batchInsertWareOverflowDetails(erpWareOverflow.getDetailsList());
        }
        return 1;
    }

    /**
     * 修改报溢
     *
     * @param erpWareOverflow 报溢
     * @return 结果
     */
    @Override
    public int updateErpWareOverflow(ErpWareOverflowDto erpWareOverflow) {
        erpWareOverflow.setApproveStatus(0);
        erpWareOverflowMapper.updateErpWareOverflow(erpWareOverflow);
        // 先删除报溢明细
        erpWareOverflowMapper.deleteWareOverflowDetailsByWareCheckId(erpWareOverflow.getWareOverflowId());
        if (erpWareOverflow.getDetailsList() != null && !erpWareOverflow.getDetailsList().isEmpty()) {
            erpWareOverflow.getDetailsList().forEach(item -> {
                item.setWareOverflowId(erpWareOverflow.getWareOverflowId());
            });
            erpWareOverflowMapper.batchInsertWareOverflowDetails(erpWareOverflow.getDetailsList());
        }
        return 1;
    }


    @Override
    @Transactional
    public int auditErpWareOverflow(ErpWareOverflow erpWareOverflow) {
        ErpWareOverflowVo wareOverflowVo = erpWareOverflowMapper.selectErpWareOverflowByWareOverflowId(erpWareOverflow.getWareOverflowId());
        if (wareOverflowVo == null) {
            throw new RuntimeException("报溢单查询失败");
        }
        wareOverflowVo.setApproveId(SecurityUtils.getUserId());
        wareOverflowVo.setApproveStatus(erpWareOverflow.getApproveStatus());
        wareOverflowVo.setApproveName(SecurityUtils.getUsername());
        wareOverflowVo.setApproveTime(DateUtils.getNowDate());
        if (wareOverflowVo.getApproveStatus() == 2) {
            wareOverflowVo.setRejectTime(DateUtils.getNowDate());
            wareOverflowVo.setRejectReason(erpWareOverflow.getRejectReason());
        }
        // 审核通过 根据盘点数量修改商品库存
        if (wareOverflowVo.getApproveStatus() == 1) {
            // 商品库存信息
            ErpWareCommodityNumber query = new ErpWareCommodityNumber();
            if (wareOverflowVo.getDetailsList() != null && !wareOverflowVo.getDetailsList().isEmpty()) {
                wareOverflowVo.getDetailsList().forEach(item -> {
                    // 根据商品id和调出仓库id查询对应库存数据
                    query.setWareId(wareOverflowVo.getWareId());
                    query.setCommodityId(item.getCommodityId());
                    ErpWareCommodityNumber erpWareCommodityNumber = erpWareCommodityNumberMapper.selectByCommodityIdAndWareId(query);
                    // 根据报损数量进行修改 库存数量 - 报损数量
                    erpWareCommodityNumber.setCommodityNumber(erpWareCommodityNumber.getCommodityNumber() + item.getCommodityNumber());
                    erpWareCommodityNumberMapper.updateErpWareCommodityNumber(erpWareCommodityNumber);

                    // 新增库存商品变更记录信息
                    ErpCommodityStockAlter erpCommodityStockAlter = StockCommodityAlterUtils.AssemblyInfo(item.getCommodityId(), BigDecimal.valueOf(item.getCommodityNumber()), "6", wareOverflowVo.getWareId());
                    erpCommodityStockAlterMapper.insertErpCommodityStockAlter(erpCommodityStockAlter);
                });
            }
        }
        return erpWareOverflowMapper.updateErpWareOverflow(wareOverflowVo);
    }

    /**
     * 批量删除报溢
     *
     * @param wareOverflowIds 需要删除的报溢主键
     * @return 结果
     */
    @Override
    public int deleteErpWareOverflowByWareOverflowIds(Long[] wareOverflowIds) {
        return erpWareOverflowMapper.deleteErpWareOverflowByWareOverflowIds(wareOverflowIds);
    }

    /**
     * 删除报溢信息
     *
     * @param wareOverflowId 报溢主键
     * @return 结果
     */
    @Override
    public int deleteErpWareOverflowByWareOverflowId(Long wareOverflowId) {
        return erpWareOverflowMapper.deleteErpWareOverflowByWareOverflowId(wareOverflowId);
    }
}
